import router from './router'
import { getToken } from '@/utils/auth' // get token from cookie
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

NProgress.configure({ showSpinner: false }) // NProgress Configuration 不显示loading


const whiteList = ['/login']

router.beforeEach((to, from, next) => {
	console.log(to);
	const hasToken = getToken()
	NProgress.start()
	// 如果已经登录
	if (hasToken) {
		if (to.path === '/login') {
			// if is logged in, redirect to the home page
			next({ path: '/' })
			NProgress.done()
		} else {
			// 权限管理 以及非规则路径先不处理 todo
			next()
		}

	} else {
		/* has no token*/
		// 如果没登录，且要去登录
		if (whiteList.indexOf(to.path) !== -1) {
			next()
		} else {
			// 如果是除了白名单路径
			// next(`/login?redirect=${to.path}`)
			next(`/login`)
		}
	}
	// NProgress.done()

})
router.afterEach(() => {
	// finish progress bar
	NProgress.done()
})
